package cn.edu.nankai.bookstore.controller.customer;

import cn.edu.nankai.bookstore.domain.po.Customer;
import cn.edu.nankai.bookstore.service.CustomerService;
import cn.edu.nankai.bookstore.service.impl.CustomerServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "FindAllUsersServlet", value = "/FindAllUsersServlet")
public class FindAllUsersServlet extends HttpServlet {
    private CustomerService customerService = new CustomerServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        if(session.getAttribute("su") == null){
            response.getWriter().write("{access:'deny'}");
            return;
        }
        List<Customer> customers = customerService.getAll();
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(customers);
        response.getWriter().write(json);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
